package com.example.ylsys.plants.database.DbOperation;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.ylsys.plants.Utils.AesUtils;
import com.example.ylsys.plants.Utils.Base64Utils;
import com.example.ylsys.plants.Utils.RSAUtils;
import com.example.ylsys.plants.Utils.SystemUtil;
import com.example.ylsys.plants.bean.LoginParamsBean;
import com.example.ylsys.plants.database.MyDatabaseHelper;

import java.security.PrivateKey;

import static com.example.ylsys.plants.Utils.RSAUtils.PRIVATE_KEY;

public class MyDbOperationUtils {
    public LoginParamsBean getLoginParams(Context context)
    {
        LoginParamsBean userParams=new LoginParamsBean();
        MyDatabaseHelper myDatabaseHelper=new MyDatabaseHelper(context,1);
        SQLiteDatabase database=myDatabaseHelper.getReadableDatabase();
        Cursor cursor= database.rawQuery("select tel_number,secret_code from login order by id desc limit 0,1",null);
        //Cursor cursor= database.query("login",new String[]{"tel_number","secret_code"},null,null,null,null,null);
        while (cursor.moveToNext()){
            userParams.setTel(cursor.getString(cursor.getColumnIndex("tel_number")));
            userParams.setCypher(cursor.getString(cursor.getColumnIndex("secret_code")));
        }
        cursor.close();
        database.close();
        // 从字符串中得到私钥
        String decryptStr=null;
        try {
            PrivateKey privateKey = RSAUtils.loadPrivateKey(PRIVATE_KEY);
            byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(userParams.getCypher()), privateKey);
            decryptStr = new String(decryptByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        userParams.setMd5Secret(decryptStr);
        userParams.setClientid(SystemUtil.getIMEI(context));
        return userParams;
    }
}
